﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using EM.Repository.utility;
using System.Data.SqlClient;

namespace EM
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {

        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {
            try
            {
                Exception ex = Server.GetLastError();

                System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(ex);
                System.Diagnostics.StackFrame[] frames = trace.GetFrames();


                foreach (var item in frames)
                {
                    int lineNumber = item.GetFileLineNumber();
                }

                if (ex != null)
                {
                    DbHelper helper = new DbHelper();
                    SqlCommand cmd = new SqlCommand();
                    string command = @"INSERT INTO TBLERROR
                                (USERID, BRANCHID, ERRORDATE, ERRORTIME, ERRORMESSAGE, INNERERRORMESSAGE, STACKTRACE, SOURCE, TARGETSITE, FIXEDFLG)
                                SELECT @USERID, @BRANCHID, @ERRORDATE, @ERRORTIME, @ERRORMESSAGE, @INNERERRORMESSAGE, @STACKTRACE, @SOURCE, @TARGETSITE, @FIXEDFLG";

                    System.Data.SqlClient.SqlParameter[] parameters = new System.Data.SqlClient.SqlParameter[10];

                    if (Session["USERID"] != null)
                        cmd.Parameters.AddWithValue("@USERID", Session["USERID"]);
                    else
                        cmd.Parameters.AddWithValue("@USERID", DBNull.Value);

                    if (Session["BRANCHID"] != null)
                        cmd.Parameters.AddWithValue("@BRANCHID", Session["BRANCHID"]);
                    else
                        cmd.Parameters.AddWithValue("@BRANCHID", DBNull.Value);

                    cmd.Parameters.AddWithValue("@ERRORDATE", DateTime.Now);
                    cmd.Parameters.AddWithValue("@ERRORTIME", DateTime.Now.ToLongTimeString());
                    cmd.Parameters.AddWithValue("@ERRORMESSAGE", ex.Message);
                    if (ex.InnerException != null)
                        if (ex.InnerException.Message != null)
                            cmd.Parameters.AddWithValue("@INNERERRORMESSAGE", ex.InnerException.Message);
                        else
                            cmd.Parameters.AddWithValue("@INNERERRORMESSAGE", DBNull.Value);
                    else
                        parameters[5] = new System.Data.SqlClient.SqlParameter("@INNERERRORMESSAGE", DBNull.Value);
                    cmd.Parameters.AddWithValue("@STACKTRACE", ex.StackTrace.ToString());
                    cmd.Parameters.AddWithValue("@SOURCE", ex.Source.ToString());
                    cmd.Parameters.AddWithValue("@TARGETSITE", ex.TargetSite.ToString());
                    cmd.Parameters.AddWithValue("@FIXEDFLG", "N");
                    cmd.CommandText = command;
                    helper.ExecuteNonQuery(cmd);

                }
            }
            catch//devam et
            {
                ;
            }
        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}